package com.qianfeng.controller;

import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Book;
import com.qianfeng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 添加图书
     * @param book
     * @return
     */
    @ResponseBody
    @RequestMapping("/addBook")
    public Result addBook(Book book){
        Result result = new Result();
        result.setCode(200);
        result.setMsg("添加图书");
        bookService.addBook(book);
        return result;
    }

    /**
     * 更新图书
     * @param book
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateBook")
    public Result updateBook(Book book){
        Result result = new Result();
        result.setCode(201);
        result.setMsg("更新图书");
        bookService.updateBook(book);
        return result;
    }

    /**
     * 删除图书
     * @param bookId
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteBook")
    public Result deleteBook(Integer bookId){
        Result result = new Result();
        result.setCode(202);
        result.setMsg("删除图书");
        bookService.deleteBook(bookId);
        return result;
    }

    @ResponseBody
    @RequestMapping("/getBookBybookId")
    public Result getBookBybookId(Integer bookId){
        Result result = new Result();
        result.setCode(203);
        result.setMsg("获得图书信息");
        Book book = bookService.getBookBybookId(bookId);
        result.setObject(book);
        return result;
    }

    @ResponseBody
    @RequestMapping("/getBookPage")
    public Page getBookPage(int pageNo,int pageSize){
        Page page = bookService.getBookPage(pageNo, pageSize);

        return page;
    }

}
